/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.various;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Luca
 */
public class Utils {

    public static String MD5pwd(String pwd) {
        String hash = "";
        try {
            MessageDigest alg=MessageDigest.getInstance("MD5");        
            alg.reset();
            alg.update(pwd.getBytes());
            byte[] dig = alg.digest();
            BigInteger bigInt = new BigInteger(1, dig);
            hash = bigInt.toString(16);
            while (hash.length() < 32) { //Padding con zeri per raggiungere i 32 char
                hash = "0" + hash;
            }
        }
        catch(NoSuchAlgorithmException ns) {
            ns.printStackTrace();
        }
        return hash;
    }
    
    public static boolean validateDate(String date){
 
        Pattern pattern = Pattern.compile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)");
        Matcher matcher = pattern.matcher(date);
 
        if(matcher.matches()){
 
            matcher.reset();

            if(matcher.find()){

                String day = matcher.group(1);
                String month = matcher.group(2);
                int year = Integer.parseInt(matcher.group(3));

                if (day.equals("31") && 
                    (month.equals("4") || month .equals("6") || month.equals("9") ||
                    month.equals("11") || month.equals("04") || month .equals("06") ||
                    month.equals("09"))) {

                        return false; // only 1,3,5,7,8,10,12 has 31 days

                } 
                else if (month.equals("2") || month.equals("02")) {
                    //leap year
                    if(year % 4==0){
                          if(day.equals("30") || day.equals("31")){
                                  return false;
                          }
                          else {
                                  return true;
                          }
                    }
                    else {
                        if(day.equals("29")||day.equals("30")||day.equals("31")){
                            return false;
                        } 
                        else {
                            return true;
                        }
                    }
                }
                else {				 
                    return true;				 
                }
            }
            else {
                return false;
	    }		  
        }
        else {
	  return false;
        }			    
    }
    
    //ritorna:
    // 1 se date1 > date2
    // 0 se date1 = date2
    //-1 se date1 < date2
    public static int compareDates(String date1, String date2) {
        String[] app = date1.split("/");
        int d1 = Integer.parseInt(app[0]);
        int m1 = Integer.parseInt(app[1]);
        int y1 = Integer.parseInt(app[2]);
        
        app = date2.split("/");
        int d2 = Integer.parseInt(app[0]);
        int m2 = Integer.parseInt(app[1]);
        int y2 = Integer.parseInt(app[2]);
        
        if(y1>y2) {
            return 1;
        }
        else if(y1<y2) {
            return -1;
        }
        else if(m1>m2) {
            return 1;
        }
        else if(m1<m2) {
            return -1;
        }
        else if(d1>d2) {
            return 1;
        }
        else if(d1<d2) {
            return -1;
        }
        else {
            return 0;
        }
    }
}
